import json

import django
import self
from django.core import serializers
from django.db.migrations import serializer
from django.http import HttpResponse, JsonResponse
from django.views import View
from django.shortcuts import render
from rest_framework.response import Response

from .models import board, UserType

# myapp/views.py
from rest_framework import viewsets, status
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from .models import Author, Book
from .serializers import *

from myproject.models import  *

def index(request):
    return HttpResponse("Hello, world! This is my first Django view.")

def show(request,id):
    return HttpResponse('app2中的show方法，参数为id，值为'+str(id))

# 视图类方式声明函数
class IndexPageView(View):
    def get(self,request):
        return HttpResponse('Get请求')
    def post(self,request):
        return HttpResponse('Post请求')

def home(request):
    data={}
    boards=board.objects.all()
    # return render(request,'home.html',{'boards':boards})

    serializers_boards=serializers.serialize('python',boards)
    fields_only = [item['fields'] for item in serializers_boards]
    data['data']=fields_only

    return JsonResponse(data)



def sss(request):
    # 列表对象
    # lists=['Java','Python','C','C#']
    #
    # #字典对象
    # dicts={'姓名':'张三','年龄':25,'性别':'男'}
     return HttpResponse('sadas')

    # return render(request,'templates/3/var.html',{'lists':lists,'dicts':dicts})




class UserTypeViewSet(viewsets.ModelViewSet):
    queryset = UserType.objects.all()
    serializer_class = UserTypeSerializer
    # permission_classes = [IsAuthenticatedOrReadOnly]




class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    # permission_classes = [IsAuthenticatedOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(user_type=self.request.data.get('user_type'))




